﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BookStore.Data
{ 
    public class adminController
    {
        static SqlConnection con = sqlDataProvider.getConnect();
        static DataTable dt;
        #region getbyID
        public static adminInfo getbyID(string username)
        {
            SqlCommand cmd = new SqlCommand("getUsernameAdm", con);
            adminInfo objAdmin = new adminInfo();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@username", username));
            SqlDataReader dr = cmd.ExecuteReader(); ;
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    objAdmin.username = dr.GetString(0).ToString();
                    objAdmin.password = dr.GetString(1).ToString();
                    objAdmin.fullname = dr.GetString(2).ToString();
                    objAdmin.birthday = dr.GetDateTime(3);
                    objAdmin.address = dr.GetString(4).ToString();
                    objAdmin.email = dr.GetString(5).ToString();
                    objAdmin.registerDate = dr.GetDateTime(6);
                    objAdmin.decentralization = dr.GetInt32(7);
                }
            }
            dr.Close();
            return objAdmin;
        }
        #endregion

        #region getbyAll
        public static DataTable ListAdm()
        {
            using (SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblAdmin ", con))
            {
                //da.SelectCommand.CommandType = CommandType.StoredProcedure;
                dt = new DataTable();
                da.Fill(dt);
            }
            return dt;
        }
        #endregion

        #region insert
        public static bool insertAdm(adminInfo objAdm)
        {
            using (SqlCommand cmd = new SqlCommand("insertAdmin", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@username", objAdm.username));
                cmd.Parameters.Add(new SqlParameter("@password", objAdm.password));
                cmd.Parameters.Add(new SqlParameter("@fullname", objAdm.fullname));
                cmd.Parameters.Add(new SqlParameter("@birthday", objAdm.birthday));
                cmd.Parameters.Add(new SqlParameter("@address", objAdm.address));
                cmd.Parameters.Add(new SqlParameter("@email", objAdm.email));
                cmd.Parameters.Add(new SqlParameter("@registerDate", objAdm.registerDate));
                cmd.Parameters.Add(new SqlParameter("@decentralization", objAdm.decentralization));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region update
        public static bool updateAdm(adminInfo objAdm)
        {
            using (SqlCommand cmd = new SqlCommand("updateAdmin", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@username", objAdm.username));
                cmd.Parameters.Add(new SqlParameter("@password", objAdm.password));
                cmd.Parameters.Add(new SqlParameter("@fullname", objAdm.fullname));
                cmd.Parameters.Add(new SqlParameter("@birthday", objAdm.birthday));
                cmd.Parameters.Add(new SqlParameter("@address", objAdm.address));
                cmd.Parameters.Add(new SqlParameter("@email", objAdm.email));
                cmd.Parameters.Add(new SqlParameter("@registerDate", objAdm.registerDate));
                cmd.Parameters.Add(new SqlParameter("@decentralization", objAdm.decentralization));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region delete
        public static bool deleteAdm(string username)
        {
            using (SqlCommand cmd = new SqlCommand("deleteAdmin", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@username", username));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion


        #region checkuser
        public bool CheckUser(adminInfo admin)
        {

            using (SqlCommand command = new SqlCommand("adCheckLogin", con))
            {

                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@username", admin.username));
                command.Parameters.Add(new SqlParameter("@password", admin.password));


                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
        #endregion
        #region checkusername
        public static bool checkUsername(string username)
        {
            bool test = false;
            SqlCommand cmd = new SqlCommand("checkUsername", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@username", username));
            SqlDataReader dr = cmd.ExecuteReader(); ;
            if (dr.HasRows)
            {
                test = true;
            }
            dr.Close();
            return test;
        }
        #endregion

        #region checkDecen
        public static int checkDecen(string username)
        {
            SqlCommand cmd = new SqlCommand("checkDecen", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@username", username));
            SqlDataReader dr = cmd.ExecuteReader(); ;
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    return dr.GetInt32(0);
                }
            }
            dr.Close();
            return 0;
        }
        #endregion
    }
}

  